﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Text;

namespace Hotnet.Web.MVCExtension
{
	public static class HtmpHelperExtension
	{
		public static string CheckBoxRadioGroup(this HtmlHelper helper, string name, SelectList list, int columns, string controlSpace, HtmlControlType controlType)
		{
			return CheckBoxRadioGroup(helper, name, list, null, columns, null, controlSpace, controlType, null);
		}

		public static string CheckBoxRadioGroup(this HtmlHelper helper, string name, SelectList list, int columns, HtmlControlType controlType)
		{
			return CheckBoxRadioGroup(helper, name, list, null, columns, null, null, controlType, null);
		}

		public static string CheckBoxRadioGroup(this HtmlHelper helper, string name, SelectList list, List<string> selectedValues, int columns, HtmlControlType controlType)
		{
			return CheckBoxRadioGroup(helper, name, list, selectedValues, columns, null, null, controlType, null);
		}

		public static string CheckBoxRadioGroup(this HtmlHelper helper, string name, SelectList list, List<string> selectedValues, int columns, object extenalDivAttributes, string controlSpace, HtmlControlType controlType)
		{
			return CheckBoxRadioGroup(helper, name, list, selectedValues, columns, extenalDivAttributes, controlSpace, controlType, null);
		}
		public static string CheckBoxRadioGroup(this HtmlHelper helper, string name, SelectList list, List<string> selectedValues, int columns, object extenalDivAttributes, string controlSpace, HtmlControlType controlType,List<string> extendAttributes)
		{
			if (list == null)
				return "";
			StringBuilder stringBuilder = new StringBuilder();
			StringBuilder innerBuilder = new StringBuilder();
			TagBuilder labelBuilder = new TagBuilder("label");
			TagBuilder clearDiv = new TagBuilder("div");
			clearDiv.MergeAttribute("style", "clear:both;");

			int i = 0;
			foreach (var item in list)
			{
				TagBuilder radioBuilder = new TagBuilder("input");
				radioBuilder.MergeAttribute("name", name);
				radioBuilder.MergeAttribute("type", controlType.ToString());
				radioBuilder.MergeAttribute("id", name + i, true);
				string boolValue = null;
				if (item.Text == "是")
					boolValue = "true";
				if (item.Text == "否")
					boolValue = "false";
				radioBuilder.MergeAttribute("value", boolValue ?? item.Value, true);
				radioBuilder.MergeAttribute("text", item.Text, true);
				if (extendAttributes != null)
				{
					radioBuilder.MergeAttribute("extend",extendAttributes[i]);
				}

				if (selectedValues != null)
					if (selectedValues.Contains(item.Value))
						radioBuilder.MergeAttribute("checked", "checked", true);

				labelBuilder.MergeAttribute("for", name + i, true);
				labelBuilder.InnerHtml = item.Text;
				if ((i + 1) % columns == 0)
				{
					TagBuilder divBuilder1 = new TagBuilder("div");
					divBuilder1.MergeAttribute("style", "float:left;" + (controlSpace == null ? "" : "width:" + controlSpace));
					divBuilder1.InnerHtml = radioBuilder.ToString() + labelBuilder.ToString();
					innerBuilder.AppendLine(divBuilder1.ToString());

					TagBuilder divBuilder2 = new TagBuilder("div");
					divBuilder2.InnerHtml = innerBuilder.ToString();
					stringBuilder.Append(divBuilder2.ToString());
					stringBuilder.Append(clearDiv.ToString());
					innerBuilder.Remove(0, innerBuilder.Length);
				}
				else
				{
					TagBuilder divBuilder = new TagBuilder("div");
					divBuilder.MergeAttribute("style", "float:left;" + (controlSpace == null ? "" : "width:" + controlSpace));
					divBuilder.InnerHtml = radioBuilder.ToString() + labelBuilder.ToString();
					innerBuilder.AppendLine(divBuilder.ToString());
				}
				i++;
			}
			if (innerBuilder.Length != 0)
			{
				TagBuilder divBuilder = new TagBuilder("div");
				divBuilder.InnerHtml = innerBuilder.ToString();
				stringBuilder.Append(divBuilder.ToString());
			}
			TagBuilder externalBuilder = new TagBuilder("div");
			externalBuilder.MergeAttributes(new RouteValueDictionary(extenalDivAttributes));
			externalBuilder.InnerHtml = stringBuilder.ToString() + clearDiv.ToString();

			return externalBuilder.ToString();
		}


	}

	public enum HtmlControlType
	{
		radio = 1,
		checkbox = 2
	}
	
}
